import Joi from 'joi'

export const schemaRegister = Joi.object({
  campaign: Joi.object({
    name: Joi.string().required(),

    status: Joi.string().required().valid('ACTIVE', 'PAUSED'),

    objective: Joi.string().required().valid('OUTCOME_SALES', 'OUTCOME_ENGAGEMENT'),

    special_ad_categories: Joi.array().items(Joi.string().valid('NONE')).required(),

    buying_type: Joi.string().valid('AUCTION').required(),

    smart_promotion_type: Joi.string().valid('GUIDED_CREATION', 'AUTOMATED_SHOPPING_ADS'),

    daily_budget: Joi.string(),

    lifetime_budget: Joi.string(),

    is_budget_schedule_enabled: Joi.boolean().when('smart_promotion_type', {
      is: 'GUIDED_CREATION',
      then: Joi.required()
    }),

    bid_strategy: Joi.string()
      .valid('COST_CAP', 'LOWEST_COST_WITHOUT_CAP', 'LOWEST_COST_WITH_BID_CAP')
      .when('is_budget_schedule_enabled', {
        is: true,
        then: Joi.required(),
        otherwise: Joi.forbidden()
      }),

    budget_type: Joi.string().valid('daily_budget', 'lifetime_budget').when('is_budget_schedule_enabled', {
      is: true,
      then: Joi.required(),
      otherwise: Joi.string()
    }),

    daily_budget: Joi.string().when('budget_type', {
      is: 'daily_budget',
      then: Joi.required(),
      otherwise: Joi.forbidden()
    }),

    lifetime_budget: Joi.string().when('budget_type', {
      is: 'lifetime_budget',
      then: Joi.required(),
      otherwise: Joi.forbidden()
    })
  }),
  adgroup: Joi.object({
    name: Joi.string().required(),

    destination_type: Joi.string().required().valid('WEBSITE', 'APP', 'UNDEFINED'),

    optimization_goal: Joi.string().valid('OFFSITE_CONVERSIONS'),

    promoted_object: Joi.object({
      // pixel_id: Joi.string().required(),
      custom_event_type: Joi.string().valid(
        'ADD_TO_WISHLIST',
        'ADD_TO_CART',
        'INITIATE_CHECKOUT',
        'COMPLETE_REGISTRATION',
        'START_TRIAL',
        'DONATE',
        'SEARCH',
        'VIEW_CONTENT',
        'ADD_PAYMENT_INFO',
        'SUBSCRIBE',
        'PURCHASE'
      )
    }),

    attribution_spec: Joi.array().items(
      Joi.object({
        event_type: Joi.string().valid('CLICK_THROUGH', 'VIEW_THROUGH', 'ENGAGED_VIDEO_VIEW'),
        window_days: Joi.number()
      })
    ),

    bid_strategy: Joi.when(Joi.ref('/campaign.is_budget_schedule_enabled'), {
      is: Joi.alternatives(Joi.valid(false), Joi.forbidden()),
      then: Joi.string().valid('LOWEST_COST_WITHOUT_CAP', 'COST_CAP').required(),
      otherwise: Joi.forbidden()
    }),

    bid_amount: Joi.when(Joi.ref('/campaign.is_budget_schedule_enabled'), {
      is: true,
      then: Joi.when(Joi.ref('/campaign.bid_strategy'), {
        is: Joi.valid('LOWEST_COST_WITH_BID_CAP', 'COST_CAP'),
        then: Joi.number().required(),
        otherwise: Joi.forbidden()
      }),
      otherwise: Joi.alternatives(Joi.number(), Joi.forbidden())
    }),

    billing_event: Joi.string().valid('IMPRESSIONS').required(),

    budget_type: Joi.string()
      .valid('daily_budget', 'lifetime_budget')
      .when(Joi.ref('/campaign.is_budget_schedule_enabled'), {
        is: Joi.valid(false),
        then: Joi.required(),
        otherwise: Joi.when(Joi.ref('/campaign.smart_promotion_type'), {
          is: 'AUTOMATED_SHOPPING_ADS',
          then: Joi.required(),
          otherwise: Joi.forbidden()
        })
      }),

    daily_budget: Joi.string().required().when('budget_type', {
      is: 'daily_budget',
      then: Joi.string().required(),

      otherwise: Joi.forbidden()
    }),

    lifetime_budget: Joi.string().required().when('budget_type', {
      is: 'lifetime_budget',
      then: Joi.string(),
      otherwise: Joi.forbidden()
    }),

    start_time: Joi.string().required(),

    end_time: Joi.string().required().allow(''),

    adset_schedule: Joi.string(),

    pacing_type: Joi.array().items(Joi.string().valid('day_parting')),

    targeting: Joi.object({
      targeting_automation: Joi.object().when(Joi.ref('/campaign/smart_promotion_type'), {
        is: 'GUIDED_CREATION',
        then: Joi.object({
          advantage_audience: Joi.boolean().required()
        })
      }),

      geo_locations: Joi.object({
        countries: Joi.array().items(Joi.string()).required(),
        genders: Joi.number().when(Joi.ref('/campaign/smart_promotion_type'), {
          is: 'GUIDED_CREATION',
          then: Joi.required(),
          otherwise: Joi.forbidden()
        }),
        locales: Joi.array().items(Joi.number()).when(Joi.ref('/campaign/smart_promotion_type'), {
          is: 'GUIDED_CREATION',
          then: Joi.required(),
          otherwise: Joi.forbidden()
        })
      }),

      age_range: Joi.array()
        .items(Joi.number())
        .when(Joi.ref('/campaign/smart_promotion_type'), {
          is: 'GUIDED_CREATION',
          then: Joi.when('targeting_automation.advantage_audience', {
            is: 1,
            then: Joi.required()
          }),
          otherwise: Joi.forbidden()
        }),

      age_min: Joi.number().when(Joi.ref('/campaign/smart_promotion_type'), {
        is: 'GUIDED_CREATION',
        then: Joi.when('targeting_automation.advantage_audience', {
          is: 1,
          then: Joi.required()
        }),
        otherwise: Joi.forbidden()
      }),

      age_max: Joi.number().when(Joi.ref('/campaign/smart_promotion_type'), {
        is: 'GUIDED_CREATION',
        then: Joi.when('targeting_automation.advantage_audience', {
          is: 1,
          then: Joi.required()
        }),
        otherwise: Joi.forbidden()
      })
    })
  }).unknown(true),
  adcreative: Joi.object({
    name: Joi.string().required(),
    ads_set: Joi.string().required().valid('create', 'post'),
    ad_format: Joi.string().required().valid('SINGLE_IMAGE_OR_VIDEO', 'CAROUSEL'),
    meta_data: Joi.object({
      ad_format: Joi.string().required().valid('SINGLE_IMAGE_OR_VIDEO', 'CAROUSEL'),
      call_to_action: Joi.object({
        type: Joi.string().required()
      })
    })

    // link_url: Joi.string().required(),

    // call_to_action_type: Joi.string().required().valid('SHOP_NOW', 'LEARN_MORE', 'SIGN_UP', 'DOWNLOAD', 'NO_BUTTON'),

    // page_id: Joi.string().required()
  }).unknown(true)
})
